import axios from 'axios';
import { getToken } from '../utils/auth';

// 创建axios实例
const api = axios.create({
  baseURL: '/api/system-settings',
  timeout: 10000
});

// 请求拦截器
api.interceptors.request.use(
  config => {
    const token = getToken();
    if (token) {
      config.headers['Authorization'] = `Bearer ${token}`;
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

/**
 * 获取所有系统设置
 */
export const getAllSettings = async () => {
  try {
    const response = await api.get('/');
    return response.data;
  } catch (error) {
    console.error('获取所有系统设置失败:', error);
    throw error;
  }
};

/**
 * 获取特定类别的系统设置
 * @param {string} category 设置类别
 */
export const getSettingsByCategory = async (category) => {
  try {
    const response = await api.get(`/${category}`);
    return response.data;
  } catch (error) {
    console.error(`获取${category}类别的系统设置失败:`, error);
    throw error;
  }
};

/**
 * 更新系统设置
 * @param {string} category 设置类别
 * @param {object} settings 设置数据
 */
export const updateSettings = async (category, settings) => {
  try {
    const response = await api.put(`/${category}`, settings);
    return response.data;
  } catch (error) {
    console.error('更新系统设置失败:', error);
    throw error;
  }
};

/**
 * 删除系统设置
 * @param {string} category 设置类别
 * @param {array} keys 要删除的设置键列表
 */
export const deleteSettings = async (category, keys) => {
  try {
    const response = await api.delete(`/${category}`, { data: { keys } });
    return response.data;
  } catch (error) {
    console.error('删除系统设置失败:', error);
    throw error;
  }
}; 